{
 "cells": [
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 实现数据的可视化展示  可以利用pyecharts实现 \n",
    "# 安装pyecharts  pip  install   pyecharts"
   ],
   "id": "12a3f8eac1068c1d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import pyecharts\n",
    "    \n",
    "print(pyecharts.__version__)  # 2.0.8"
   ],
   "id": "1e8b4368e729605f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 快速上手  柱状图\n",
    "from pyecharts.charts import Bar\n",
    "\n",
    "# 创建柱状图实例\n",
    "bar = Bar()\n",
    "bar.add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "bar.add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "bar.add_yaxis(\"商家B\", [15, 120, 136, 110, 170, 190])\n",
    "bar.add_yaxis(\"商家C\", [25, 220, 236, 210, 270, 290])\n",
    "# render 会生成本地 HTML 文件，默认会在当前目录生成 render.html 文件\n",
    "# 也可以传入路径参数，如 bar.render(\"mycharts.html\")\n",
    "# bar.render(\"bar.html\")\n",
    "# bar.render()\n",
    "bar.render_notebook()  # 将图表直接渲染在notebook文件中"
   ],
   "id": "3f18b471cc1beddd",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# pyecharts 所有方法均支持链式调用  每个方法调用完毕后 均会将当前实例返回  通过源码可以看到 return  self\n",
    "bar = (\n",
    "    Bar()\n",
    "    .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "    .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "    .add_yaxis(\"商家B\", [15, 120, 136, 110, 170, 190])\n",
    ")\n",
    "\n",
    "bar.render_notebook()"
   ],
   "id": "c66f9af98d6890a6",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 图表的设置  初始化配置   全局配置 \n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMA, width=\"1080px\", height=\"520px\"))\n",
    "    .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "    .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "    .add_yaxis(\"商家B\", [15, 120, 136, 110, 170, 190])\n",
    "    .set_global_opts(  # 全局配置项\n",
    "        title_opts=opts.TitleOpts(\n",
    "            is_show=True,  # 是否展示标题\n",
    "            title='主标题',\n",
    "            title_link='http://www.baidu.com',  # 设置标题链接\n",
    "            title_target='blank',  # 链接打开方式\n",
    "            title_textstyle_opts=opts.TextStyleOpts(\n",
    "                color='red',\n",
    "                font_family='黑体',\n",
    "                font_size=30,\n",
    "            ),\n",
    "            subtitle='副标题',\n",
    "            subtitle_link='http://www.jd.com',\n",
    "            subtitle_target='blank',\n",
    "            subtitle_textstyle_opts=opts.TextStyleOpts(\n",
    "                color='green',\n",
    "                font_family='楷体',\n",
    "                font_size=20,\n",
    "            ),\n",
    "            pos_right='50',\n",
    "            item_gap=30  # 主副标题之间的间隔\n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "bar.render_notebook()"
   ],
   "id": "5aba6a84ea91b825",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 区域缩放设置\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMA, width=\"1080px\", height=\"520px\"))\n",
    "    .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "    .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "    .add_yaxis(\"商家B\", [15, 120, 136, 110, 170, 190])\n",
    "    .set_global_opts(  # 全局配置项    缩放区域\n",
    "        datazoom_opts=opts.DataZoomOpts(\n",
    "            is_show=True,  # 是否展示缩放区域\n",
    "            range_start=0,  # 起始位置\n",
    "            range_end=50,  # 结束位置\n",
    "            is_zoom_lock=True,  # 锁定缩放区域\n",
    "            orient=\"horizontal\"  # 改变缩放区域渲染的方向    vertical  纵向\n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "bar.render_notebook()"
   ],
   "id": "e877f0f9be89a51",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 图例配置  \n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMA, width=\"1080px\", height=\"520px\"))\n",
    "    .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "    .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "    .add_yaxis(\"商家B\", [15, 120, 136, 110, 170, 190])\n",
    "    .add_yaxis(\"商家C\", [25, 220, 236, 210, 270, 290])\n",
    "    .add_yaxis(\"商家D\", [35, 320, 336, 310, 370, 390])\n",
    "    .set_global_opts(  # 全局配置项    图例配置\n",
    "        legend_opts=opts.LegendOpts(\n",
    "            type_='scroll',  # 图例类型  plain  scroll  翻页\n",
    "            is_show=True,  # 是否显示图例\n",
    "            pos_left=10,  # 图例位置\n",
    "            orient='vertical',  # 设置图例的方向   horizontal\n",
    "            selected_mode='multiple',  # 设置图例是否可以点击  True  False  single  multiple\n",
    "            align='left',  # 设置图表和文字的位置\n",
    "            item_gap=40,  # 图例之间的间距\n",
    "            # item_width=30,  # 图例宽度\n",
    "            # item_height=35,  # 图例高度\n",
    "            inactive_color='black',  # 设置未激活的颜色\n",
    "            legend_icon='triangle',  # 设置图例的图标  circle  rect roundRect  triangle   \n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "bar.render_notebook()"
   ],
   "id": "994e12aef36e0012",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 视觉映射配置项\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMA, width=\"1080px\", height=\"520px\"))\n",
    "    .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "    .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "    .add_yaxis(\"商家B\", [15, 120, 136, 110, 170, 190])\n",
    "    .set_global_opts(  # 全局配置项    图例配置\n",
    "        visualmap_opts=opts.VisualMapOpts(\n",
    "            is_show=True,  # 是否显示视觉映射\n",
    "            type_='color',  # 视觉类型  color  size\n",
    "            max_=300,\n",
    "            min_=0,\n",
    "            range_text=['max', 'min'],  # 两端文本\n",
    "            range_color=['yellow', 'green', 'red'],  # 设置颜色\n",
    "            is_piecewise=True,  # 是否为分段形式\n",
    "            pos_right='1%',\n",
    "            pos_top='50px',\n",
    "            is_inverse=True  # 是否翻转\n",
    "        ),\n",
    "        # 提示信息配置项\n",
    "        tooltip_opts=opts.TooltipOpts(\n",
    "            is_show=True,  # 开启提示\n",
    "            trigger_on='mousemove'  # 触发的形式 click  mousemove\n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "bar.render_notebook()"
   ],
   "id": "58b5d3bcbadb7f55",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#  坐标轴设置\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMA, width=\"1080px\", height=\"520px\"))\n",
    "    .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "    .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "    .add_yaxis(\"商家B\", [15, 120, 136, 110, 170, 190])\n",
    "    .set_global_opts(  # 全局配置项  坐标轴配置\n",
    "        xaxis_opts=opts.AxisOpts(\n",
    "            is_show=True,\n",
    "            is_inverse=False,  # 翻转x轴的类目信息\n",
    "        ),\n",
    "        yaxis_opts=opts.AxisOpts(\n",
    "            is_show=True,\n",
    "            axisline_opts=opts.AxisLineOpts(\n",
    "                is_show=True,  #是否显示轴线\n",
    "            ),\n",
    "            axistick_opts=opts.AxisTickOpts(\n",
    "                is_show=True,  #是否显示刻度\n",
    "            )\n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "bar.render_notebook()"
   ],
   "id": "b28f6704de5ef80c",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 系列配置项\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMA, width=\"1080px\", height=\"520px\"))\n",
    "    .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "    .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "    .set_series_opts(\n",
    "        itemstyle_opts=opts.ItemStyleOpts(\n",
    "            color='pink',\n",
    "            opacity=0.7,\n",
    "            border_width=1,\n",
    "            border_color='black',\n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "bar.render_notebook()"
   ],
   "id": "49d89def4451de84",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "7cd24950e45aa854",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
